package mh.modules.mh.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import mh.modules.mh.entity.WeatherEntity;
import mh.modules.mh.service.WeatherService;
import mh.common.utils.PageUtils;
import mh.common.utils.R;



/**
 * 
 *
 * @author zhongxiang
 * @email 
 * @date 2024-10-23 16:59:34
 */
@RestController
@RequestMapping("mh/weather")
@Slf4j
public class WeatherController {
    @Autowired
    private WeatherService weatherService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mh:weather:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = weatherService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mh:weather:info")
    public R info(@PathVariable("id") Long id){
		WeatherEntity weather = weatherService.getById(id);

        return R.ok().put("weather", weather);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mh:weather:save")
    public R save(@RequestBody WeatherEntity weather){
//        log.info("WeatherEntity{}",weather);
        List<WeatherEntity> list = weatherService.list(null);
        int gWdays =0;
        int disasterDays=0;
        for(WeatherEntity weatherEntity : list){
            if(weatherEntity.getTemperature()>=39 || weatherEntity.getTemperature()<=0){
                gWdays++;
            }
            if((weatherEntity.getTemperature()>=39 || weatherEntity.getTemperature()<=0)||(weatherEntity.getPrecipitation()>=80 ||weatherEntity.getPrecipitation()<=20 )){
                disasterDays++;
            }
        }
        for(WeatherEntity weatherEntity : list){
            weatherEntity.setGwDays(gWdays); // 高温天气
            weatherEntity.setDisasterDays(disasterDays);  //灾害天气
            weatherService.updateById(weatherEntity);
        }
        for(WeatherEntity weatherEntity : list){
            // 如果今天存了天气就不存了
            if(weatherEntity.getRecordDate().toString().equals(weather.getRecordDate().toString())){
                return R.error("今天已经存了");
            }
        }
        weatherService.save(weather);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mh:weather:update")
    public R update(@RequestBody WeatherEntity weather){
		weatherService.updateById(weather);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mh:weather:delete")
    public R delete(@RequestBody Long[] ids){
		weatherService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 天气折线图（一周的）当前日期往前推七天
     * 大于39小于0 || 湿度大于80 小于20 为灾害天气
     * 相对湿度低于 20% 时，空气通常感觉干燥
     * 相对湿度在 20% - 80% 之间时，
     * 相对湿度高于 80% 时，空气会感觉比较潮湿
     */
    @RequestMapping("/temperature")
    public R temperature(){
        R r = weatherService.getWeeklyTemperature();
        return R.ok(r);
    }


}
